package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;

import com.cskaoyan.vo.*;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxCouponServiceImpl implements WxCouponService{

    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;
    @Override
    public CouponVo_wx queryMyList(Integer page, Integer status, Integer size) {

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        //用戶id
        Integer userId = user.getId();
        //分页
        PageHelper.startPage(page,size);
        //查询
        CouponUserExample couponUserExample = new CouponUserExample();
        //根据status和userId查询用户的所有couponId，
        couponUserExample.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(status);
        //得到所有用户，遍历所以couponId放入arrayList
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);

        ArrayList<Integer> integers = new ArrayList<>();

        for (CouponUser couu:couponUsers) {
            Integer couponId = couu.getCouponId();
            integers.add(couponId);
        }
        BigDecimal totalPrice = new BigDecimal(Integer.MAX_VALUE);

        List<CouponListMsgVo> coupons = couponMapper.selectCouponByIds(integers,totalPrice);


        PageInfo<CouponListMsgVo> pageInfo = new PageInfo<>(coupons);
        long total = (int) pageInfo.getTotal();

        CouponVo_wx couponVo_wx = new CouponVo_wx();

        couponVo_wx.setCount(total);

        couponVo_wx.setData(coupons);

        return couponVo_wx;
    }
    @Override
    public void exchangeCoupoon(String code) {

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Integer userId = user.getId();

        CouponUserExample couponUserExample = new CouponUserExample();

        couponUserExample.createCriteria().andUserIdEqualTo(userId);

        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);

        CouponUser couponUser = couponUsers.get(0);

        CouponExample couponExample = new CouponExample();

        couponExample.createCriteria().andTypeEqualTo((short) 2);

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        for (Coupon item : coupons) {
            if(item.getCode()!=null&&item.getCode().equals(code)){
                item.setType((short) 1);
                couponMapper.updateByPrimaryKey(item.getId());
                couponUser.setUpdateTime(new Date());
                couponUser.setStatus((short) 1);
                couponUserMapper.updateByPrimaryKey(couponUser);
            }
        }
    }
    @Override
    public Integer receiveCoupon(Integer couponId) {


        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Integer userId = user.getId();
        //查询用户
        CouponUser couponUser = couponUserMapper.selectByPrimaryKey(userId);

//        CouponUserExample couponUserExample = new CouponUserExample();
//
//       couponUserExample.createCriteria().andIdEqualTo(userId);
//
//        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
//
//        CouponUser couponUser = couponUsers.get(0);

        //查询优惠卷种类
//        Coupon coupons = couponMapper.selectByPrimaryKey(couponId);
//        if(coupons.getTotal()!=0){
//            Integer total = coupons.getTotal();
//            total--;
//            coupons.setTotal(total);
//            couponMapper.updateByPrimaryKeySelective(coupons);
//            couponUser.setStatus((short) 0);
//            couponUserMapper.updateByPrimaryKeySelective(couponUser);
//        }


        Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
        if (coupon == null){
            return 1;
        }
        Integer total = coupon.getTotal();
        if (total < 0){

            return 1;
        }
        User users = (User) SecurityUtils.getSubject().getPrincipal();
        Integer userIds = user.getId();
        Integer x = getInteger(coupon,userId);
        return x;

    }
    private Integer getInteger(Coupon coupon, Integer userId) {
        Integer couponId = coupon.getId();
        Integer total = coupon.getTotal();

        Short limit = coupon.getLimit();

        Integer count = couponUserMapper.selectCouponCountByUidAndCid(userId,couponId);

        if (limit <= count && limit != 0){
            return 2;
        }

        if (limit == 0 || limit > count){
            Short status = 0;
            CouponUser couponUser = new CouponUser(null,userId, couponId,status,null,
                    coupon.getStartTime(),coupon.getEndTime(),null,new Date(),null,false);
            //更新用户优惠券表
            couponUserMapper.insertSelective(couponUser);

        }


        if (total == 0){
            return 0;
        }
        coupon.setUpdateTime(new Date());
        if (total == 1){
            coupon.setTotal(-1);
        }else {
            coupon.setTotal(total - 1);
        }

        couponMapper.updateByPrimaryKeySelective(coupon);
        return 0;
    }

    @Override
    public WxVo queryList(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<CouponListMsgVo> dataBeans = couponMapper.selectAllCoupon();
        PageInfo<CouponListMsgVo> tPageInfo = new PageInfo<>(dataBeans);
        int total = (int) tPageInfo.getTotal();
        WxVo wxVo = new WxVo();
        wxVo.setCount(total);
        wxVo.setData(dataBeans);
        return wxVo;
    }

//    @Override
////        List<ListWxVo> listWxVos = couponMapper.selectByList(couponExample);
////
////        PageInfo<ListWxVo> listWxVoPageInfo = new PageInfo<>(listWxVos);
////
////        long total = listWxVoPageInfo.getTotal();
////
//        PageHelper.startPage(page,size);
//        List<CouponListMsgVo> dataBeans = couponMapper.selectAllCoupon();
//        PageInfo<CouponListMsgVo> tPageInfo = new PageInfo<>(dataBeans);
//        int total = (int) tPageInfo.getTotal();
//        WxVo wxVo = new WxVo();
//        wxVo.setCount((int) total);
//        wxVo.setData(dataBeans);
//        return wxVo;
//    }

    @Override
    public List<CouponListMsgVo> selectList( Integer cartId, Integer grouponRulesId, Integer grouponRulesId1, Integer addressId) {

        //先查询 购物车总价格
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Integer userId = user.getId();


          return null;
    }


}
